/*
 * Copyright (C) 2012 YIXIA.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cntv.player.core.util;

import java.util.Arrays;
import java.util.Iterator;

public class StringUtil {
	public static String join(Object[] elements, CharSequence separator) {
		return join(Arrays.asList(elements), separator);
	}

	public static String join(Iterable<? extends Object> elements, CharSequence separator) {
		StringBuilder builder = new StringBuilder();

		if (elements != null) {
			Iterator<? extends Object> iter = elements.iterator();
			if (iter.hasNext()) {
				builder.append(String.valueOf(iter.next()));
				while (iter.hasNext()) {
					builder.append(separator).append(String.valueOf(iter.next()));
				}
			}
		}

		return builder.toString();
	}

	public static String fixLastSlash(String str) {
		String res = str == null ? "/" : str.trim() + "/";
		if (res.length() > 2 && res.charAt(res.length() - 2) == '/')
			res = res.substring(0, res.length() - 1);
		return res;
	}

	public static int convertToInt(String str) throws NumberFormatException {
		int s, e;
		for (s = 0; s < str.length(); s++)
			if (Character.isDigit(str.charAt(s)))
				break;
		for (e = str.length(); e > 0; e--)
			if (Character.isDigit(str.charAt(e - 1)))
				break;
		if (e > s) {
			try {
				return Integer.parseInt(str.substring(s, e));
			} catch (NumberFormatException ex) {
				throw new NumberFormatException();
			}
		} else {
			throw new NumberFormatException();
		}
	}

	public static String generateTime(long time) {
		if(time < 1000){
			return "00:00";
		}
		int totalSeconds = (int) (time / 1000);
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		int hours = totalSeconds / 3600;

		return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
	}
	
	/**
	 * 获取视频长度
	 * @param len  00:45:18
	 * @return  长度 秒值
	 */
	public static int getVideoLen(String len) {
		try {
			int hour = Integer.parseInt(len.split(":")[0]) * 60 * 60;
			int min = Integer.parseInt(len.split(":")[1]) * 60;
			int sec = Integer.parseInt(len.split(":")[2]);
			return hour + min + sec;
		} catch (Exception e) {
			return 0;
		}
	}
	
	/**
	 * 组装不同码率播放地址
	 * @param hlsUrl 主地址
	 * @param hlsSubUrl 不同码率地址
	 * @return
	 */
	public static String fitVodModeUrl(String hlsUrl, String hlsSubUrl) { 
		if (hlsUrl == null || "".equals(hlsUrl.trim())) { 
			return null;
		}

		if (!hlsUrl.startsWith("http://") && !hlsUrl.startsWith("https://")) {
			return null;
		}
		//    http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/6295d93f4e964903bc5bd89ab8d35b5f/main.m3u8
		String protocol = hlsUrl.substring(0, hlsUrl.indexOf(":"));//http
		String strtoken = hlsUrl.substring(hlsUrl.indexOf("/") + 2);
		int domainIdx = strtoken.indexOf("/");
		
		String domainName = "";
		if (domainIdx != -1) {
			domainName = strtoken.substring(0, domainIdx); //       asp.cntv.lxdns.com
		} else{
			return null;
		}

		String realUrl = null;
		if (hlsSubUrl.startsWith("http://") || hlsSubUrl.startsWith("https://")) {
			// contain whole real url
			realUrl = hlsSubUrl;
		} else if (hlsSubUrl.startsWith("/")) {
			// domain + subUrl;
			realUrl = protocol + "://" + domainName + hlsSubUrl;
		} else {
			// mainUrl + subUrl;
			realUrl = hlsUrl.substring(0, hlsUrl.lastIndexOf("/")) + "/" + hlsSubUrl;
		}
//		LogUtil.i("zl","realUrl = "  + realUrl);
		return realUrl;
	}
	
}
